


package com.aglhz.nature.fragment;







import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import com.aglhz.nature.R;
import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.modle.BGBean;
import com.aglhz.nature.presenter.MyselfPresenter;
import com.aglhz.nature.utils.AsyncHttpCilentUtil;
import com.aglhz.nature.utils.CheckTokenHelper;
import com.aglhz.nature.utils.Constants;
import com.aglhz.nature.view.ArrowDetailsWebActivity;
import com.aglhz.nature.view.ChangePesonDataActivity;
import com.aglhz.nature.view.IntroduceActivity;
import com.aglhz.nature.view.LoginActivity;
import com.aglhz.nature.view.PersonDataActivity;
import com.aglhz.nature.view.SetActivity;
import com.aglhz.nature.view.iv.MyselfView;
import com.aglhz.nature.view.touxiang.CircularImage;
import com.aglhz.nature.view.weixin.WeiXinLoginActivity;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.utils.Log;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.cookie.Cookie;
import cz.msebera.android.httpclient.util.TextUtils;

import android.R.integer;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.renderscript.Int2;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyselfFragment extends Fragment implements MyselfView,OnClickListener{
	private static String TAG = MyselfFragment.class.getName();
	private MyselfPresenter presenter;
	private Intent dataIntent = null;
	private BGBean bgBean;
	private String data;
	private String url;
	//
	private SharedPreferences sharedPreferences; 
	private SharedPreferences SHPreferences; 
	private SharedPreferences normalPreferences; 
	//不为空的token
	private String token;
	//微信登陆用到的数据
	private String refresh_token;
	private String openid;
	private String access_token;
	private String nickname;
	private String headimgurl;
	private String unionid;
	private String WXToken;
	
	//普通登陆用到的数据
	private String NORMAL_Token;
	private String NORMAL_Name;
	private String NORMAL_Point;
	private String NORMAL_Money;
	
	//登陆成功后显示的信息
	private CircularImage headImage;
	//积分
	private TextView integral_number;
	private TextView cardvolume_number;
	private TextView wallet_number;
	private TextView address_number;
	private TextView userName;
	private ImageView bg_image;
	
	
	//订单
	private TextView all_order;
//	private RadioButton pay_arrow;
//	private RadioButton accept_arrow;
//	private RadioButton arrow_evaluate;
//	private RadioButton back_arrow;
//	private RadioButton repair;
	private RelativeLayout rl1,rl2,rl3,rl4,rl5;
	private TextView pay_arrow_number,accept_arrow_number,arrow_evaluate_number,back_arrow_number,repair_number;
	//标识
	private String IsNofistLogIn;
	
	
	//huiyuan
	private TextView personal_data;
	private RadioButton address_management;
	//XIU CHANG
	private RelativeLayout myshow;
	private RelativeLayout dynamic_show;
	private RelativeLayout re_my_set;
	
	//推荐
//	private RadioButton iv1,iv2,iv3,iv4;
	
	
	
	public MyselfPresenter getPresenter() {
		return presenter;
	}




	@Override
	public View onCreateView(LayoutInflater inflater,
			 ViewGroup container,  Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.myselffragment, null);
		//修改头像
		url=ServerAPI.APIHEARD+ServerAPI.UPLOADIMAGEHEADER;
		bg_image=(ImageView) view.findViewById(R.id.bg_image);
		headImage=(CircularImage) view.findViewById(R.id.user_photo);
		userName=(TextView) view.findViewById(R.id.wxUserName);
		integral_number=(TextView) view.findViewById(R.id.integral_number);
		cardvolume_number=(TextView) view.findViewById(R.id.cardvolume_number);
		wallet_number=(TextView) view.findViewById(R.id.wallet_number);
		address_number=(TextView) view.findViewById(R.id.address_number);
		re_my_set=(RelativeLayout) view.findViewById(R.id.re_my_set);
	
//		iv1=(RadioButton) view.findViewById(R.id.iv1);
//		iv2=(RadioButton) view.findViewById(R.id.iv2);
//		iv3=(RadioButton) view.findViewById(R.id.iv3);
//		iv4=(RadioButton) view.findViewById(R.id.iv4);
		//订单
		pay_arrow_number=(TextView) view.findViewById(R.id.pay_arrow_number);
		accept_arrow_number=(TextView) view.findViewById(R.id.accept_arrow_number);
		arrow_evaluate_number=(TextView) view.findViewById(R.id.arrow_evaluate_number);
		back_arrow_number=(TextView) view.findViewById(R.id.back_arrow_number);
		repair_number=(TextView) view.findViewById(R.id.repair_number);
		
		all_order=(TextView) view.findViewById(R.id.all_order);
		rl1=(RelativeLayout) view.findViewById(R.id.rl1);
		rl2=(RelativeLayout) view.findViewById(R.id.rl2);
		rl3=(RelativeLayout) view.findViewById(R.id.rl3);
		rl4=(RelativeLayout) view.findViewById(R.id.rl4);
		rl5=(RelativeLayout) view.findViewById(R.id.rl5);
		personal_data=(TextView) view.findViewById(R.id.personal_data);
		address_management=(RadioButton) view.findViewById(R.id.address_management);
		myshow=(RelativeLayout) view.findViewById(R.id.myshow);
		dynamic_show=(RelativeLayout) view.findViewById(R.id.dynamic_show);
		
		all_order.setOnClickListener(this);
		rl1.setOnClickListener(this);
		rl2.setOnClickListener(this);
		rl3.setOnClickListener(this);
		rl4.setOnClickListener(this);
		rl5.setOnClickListener(this);		
		address_management.setOnClickListener(this);
		personal_data.setOnClickListener(this);
		re_my_set.setOnClickListener(this);
		myshow.setOnClickListener(this);
		dynamic_show.setOnClickListener(this);
		headImage.setOnClickListener(this);
		
//		iv1.setOnClickListener(this);
//		iv2.setOnClickListener(this);
//		iv3.setOnClickListener(this);
//		iv4.setOnClickListener(this);
		
		//获取sharedPreferences里面的信息 weixin
		sharedPreferences = getActivity().getSharedPreferences("WX_INFO", Context.MODE_PRIVATE);
		refresh_token=sharedPreferences.getString("refresh_token", "");	
		openid=sharedPreferences.getString("openid", "");
		access_token=sharedPreferences.getString("access_token", "");
		nickname=sharedPreferences.getString("nickname", "");
		headimgurl=sharedPreferences.getString("headimgurl", "");
		unionid=sharedPreferences.getString("unionid", "");
		WXToken=sharedPreferences.getString("token", "");
		//普通登陆
		normalPreferences = getActivity().getSharedPreferences("Normal_Info", Context.MODE_PRIVATE);
		NORMAL_Token=normalPreferences.getString("NORMAL_Token", "");
		NORMAL_Name=normalPreferences.getString("NORMAL_Name","");
		NORMAL_Point=normalPreferences.getString("NORMAL_POINT","");
		NORMAL_Money=normalPreferences.getString("NORMAL_MONEY","");
		
		Log.e("shujulaile -------------------",openid+"============"+NORMAL_Token);
		
		Log.e("SHPreferences--","================="+IsNofistLogIn);
		presenter = new MyselfPresenter(this,this.getActivity().getApplicationContext());
		// basePresenter unregister
		presenter.register();	
		if(!TextUtils.isEmpty(WXToken)){
			
			token=WXToken;
			Log.e("-1---",token);
		}else if(!TextUtils.isEmpty(NORMAL_Token)){
			token=NORMAL_Token;
			Log.e("-2---",token);
		}else{
			token="";
		}
		presenter.loginCheck(token);

			
		
		

		
		
		return view;
	}
	



	//ding dan dian ji
	@Override
	public void onClick(View arg0) {
		
		
		
		
		switch (arg0.getId()) {
		case R.id.all_order:
			Intent intent;
		if(!TextUtils.isEmpty(token)){
			 intent=new Intent(getActivity(),ArrowDetailsWebActivity.class);
			String all_order_url="http://h5.aglhz.com/gc_community/m/html/allOrder.html?type=1&token="+token+"&appType=2";
			intent.putExtra("all_order", all_order_url);
			startActivity(intent);
		}else{
			intent =new Intent(getActivity(),LoginActivity.class);
			intent.putExtra("MyselfFragmentFlag", 1);
			startActivityForResult(intent, Constants.TOLOGIN);
			
		}
			
			
			break;
		case R.id.rl1:
			Intent intent1;
			if(!TextUtils.isEmpty(token)){
				intent1=new Intent(getActivity(),ArrowDetailsWebActivity.class);
				String pay_arrow_url="http://h5.aglhz.com/gc_community/m/html/allOrder.html?type=2&token="+token+"&appType=2";
				intent1.putExtra("pay_arrow", pay_arrow_url);
				startActivity(intent1);	
			}else{
				intent1 =new Intent(getActivity(),LoginActivity.class);
				intent1.putExtra("MyselfFragmentFlag", 1);
				startActivityForResult(intent1, Constants.TOLOGIN);
				
			}
			
			break;
		case R.id.rl2:
			Intent intent2;
			if(!TextUtils.isEmpty(token)){
				intent2=new Intent(getActivity(),ArrowDetailsWebActivity.class);
				String accept_arrow_url="http://h5.aglhz.com/gc_community/m/html/allOrder.html?type=3&token="+token+"&appType=2";
				intent2.putExtra("accept_arrow", accept_arrow_url);
				startActivity(intent2);	
			}else{
				intent2 =new Intent(getActivity(),LoginActivity.class);
				intent2.putExtra("MyselfFragmentFlag", 1);
				startActivityForResult(intent2, Constants.TOLOGIN);
				
			}
			
			break;
		case R.id.rl3:
			Intent intent3;
			if(!TextUtils.isEmpty(token)){
				intent3=new Intent(getActivity(),ArrowDetailsWebActivity.class);
				String arrow_evaluate_url="http://h5.aglhz.com/gc_community/m/html/orderEvaluation.html?token="+token+"&appType=2";
				intent3.putExtra("arrow_evaluate", arrow_evaluate_url);
				startActivity(intent3);	
			}else{
				intent3 =new Intent(getActivity(),LoginActivity.class);
				intent3.putExtra("MyselfFragmentFlag", 1);
				startActivityForResult(intent3, Constants.TOLOGIN);
				
			}
		
			break;
		case R.id.rl4:
			Intent intent4;
			if(!TextUtils.isEmpty(token)){
				intent4=new Intent(getActivity(),ArrowDetailsWebActivity.class);
				String back_arrow_url="http://h5.aglhz.com/gc_community/m/html/orderOriginList.html?token="+token+"&appType=2";
				intent4.putExtra("back_arrow", back_arrow_url);
				startActivity(intent4);
			}else{
				intent4 =new Intent(getActivity(),LoginActivity.class);
				intent4.putExtra("MyselfFragmentFlag", 1);
				startActivityForResult(intent4, Constants.TOLOGIN);
				
			}
				
			break;
		case R.id.rl5:
			Intent intent5;
			intent5=new Intent(getActivity(),ArrowDetailsWebActivity.class);
			if(!TextUtils.isEmpty(token)){
				String repair_url="http://h5.aglhz.com/gc_community/m/html/refundList.html?token="+token+"&appType=2";
				intent5.putExtra("repair", repair_url);
				startActivity(intent5);		
				
			}else{
				intent5 =new Intent(getActivity(),LoginActivity.class);
				intent5.putExtra("MyselfFragmentFlag", 1);
				startActivityForResult(intent5, Constants.TOLOGIN);				
			}
			break;
		case R.id.address_management:
			Intent intent6;
			if(!TextUtils.isEmpty(token)){
				intent6=new Intent(getActivity(),ArrowDetailsWebActivity.class);
				String address_management_url="http://h5.aglhz.com/gc_community/m/html/addressee_list.html?token="+token+"&appType=2";
				intent6.putExtra("address_management", address_management_url);
				startActivity(intent6);		
			}else{
				intent6 =new Intent(getActivity(),LoginActivity.class);
				intent6.putExtra("MyselfFragmentFlag", 1);
				startActivityForResult(intent6, Constants.TOLOGIN);				
			}
			
			break;
		case R.id.personal_data:
			Intent intent7;
			
			if(!TextUtils.isEmpty(token)){
				intent7=new Intent(getActivity(),ChangePesonDataActivity.class);

				startActivity(intent7);		
			}else{
				intent7 =new Intent(getActivity(),LoginActivity.class);
				
				startActivityForResult(intent7, Constants.TOLOGIN);				
			}
			
			break;
		case R.id.re_my_set:
			Intent intent8=new Intent(getActivity(),SetActivity.class);
			startActivity(intent8);		
			break;
		case R.id.myshow:
			Intent intent9;
			if(!TextUtils.isEmpty(token)){
				intent9=new Intent(getActivity(),ArrowDetailsWebActivity.class);
				String myshow_url="http://h5.aglhz.com/gc_community/m/html/showPersonal.html?token="+token+"&appType=2";
				intent9.putExtra("myshow",myshow_url);
				startActivity(intent9);	
			}else{
				intent9 =new Intent(getActivity(),LoginActivity.class);
				intent9.putExtra("MyselfFragmentFlag", 1);
				startActivityForResult(intent9, Constants.TOLOGIN);				
			}
					
			break;
		case R.id.dynamic_show:
			Intent intent10;
			if(!TextUtils.isEmpty(token)){
				intent10=new Intent(getActivity(),ArrowDetailsWebActivity.class);
				String dynamic_show_url="http://h5.aglhz.com/gc_community/m/html/personShowdong.html?token="+token+"&appType=2";
				intent10.putExtra("showcenter",dynamic_show_url);
				startActivity(intent10);
			}else{
				intent10 =new Intent(getActivity(),LoginActivity.class);
				intent10.putExtra("MyselfFragmentFlag", 1);
				startActivityForResult(intent10, Constants.TOLOGIN);				
			}
					
			break;
		case R.id.user_photo:
			if(TextUtils.isEmpty(token)){
				Intent intent12 =new Intent(getActivity(),LoginActivity.class);
				startActivityForResult(intent12, Constants.TOLOGIN);
			}else{
			ShowPickDialog();
			}
		
			break;
		/*case R.id.iv1:
			Intent intent11=new Intent(getActivity(),IntroduceActivity.class);
			String iv1Url="http://www.youku.com";
			intent11.putExtra("iv1",iv1Url);
			startActivity(intent11);		
			break;
		case R.id.iv2:
			Intent intent12=new Intent(getActivity(),IntroduceActivity.class);
			String iv2Url="http://www.toutiao.com";
			intent12.putExtra("iv2",iv2Url);
			Log.e("---","qqqq");
			startActivity(intent12);		
			break;
		case R.id.iv3:
			Intent intent13=new Intent(getActivity(),IntroduceActivity.class);
			String iv3Url="http://www.17173.com";
			intent13.putExtra("iv3",iv3Url);
			startActivity(intent13);		
			break;
		case R.id.iv4:
			Intent intent14=new Intent(getActivity(),IntroduceActivity.class);
			String iv4Url="http://www.kugou.com";
			intent14.putExtra("iv4",iv4Url);
			startActivity(intent14);		
			break;*/
		
		default:
			break;
		}
	
		
	}
	
	
	
	
	@Override
	public String setRefresh_token() {
		return refresh_token;
	}
	@Override
	public String setOpenid() {
		return openid;
	}
	@Override
	public String setAccess_token() {
		return access_token;
	}
	@Override
	public String setUnionid() {
		return unionid;
	}
	@Override
	public String setNickname() {
		return nickname;
	}
	@Override
	public String setHeadimgurl() {
		return headimgurl;
	}


	@Override
	public void setHeadimage(String url) {
	
		Picasso.with(getActivity()).load(url)
        .error(R.drawable.ic_launcher).into(headImage);
	}
	@Override
	public void setUserName(String name) {
		String rightName;
		try {
			rightName = new String(name.getBytes("ISO-8859-1"), "utf-8");
			userName.setText(rightName);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	

		
	
	}
	
	@Override
	public void setBgImage(String url) {
		Picasso.with(getActivity()).load(url)
        .into(bg_image);
		
	}
	
	@Override
	public void setMembersInfo(int a,int b,int c,int d,int e,String in_number, String card_number,String wal_number) {
	//订单
		pay_arrow_number.setText(a+"");
		accept_arrow_number.setText(b+"");
		arrow_evaluate_number.setText(c+"");
		back_arrow_number.setText(d+"");
		repair_number.setText(e+"");
		//积分
			integral_number.setText(in_number);
			cardvolume_number.setText(card_number);
			wallet_number.setText(wal_number);			
			
		}

	@Override
	public void setAddressSize(int a) {
	address_number.setText(a);
		
	}

	
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		presenter.unregister();
	}
	@Override
	public void ClearInfo() {
		if(token==WXToken){
			sharedPreferences.edit().clear().commit(); 
		}else if(token==NORMAL_Token){
			normalPreferences.edit().clear().commit(); 
		}
		
	}
	@Override
	public void SaveInfo() {
		if(token==WXToken){
			sharedPreferences = getActivity().getSharedPreferences("WX_INFO",Context.MODE_PRIVATE);  
	         sharedPreferences.edit()
	         .putString("refresh_token",refresh_token)
	         .putString("openid",openid)
	         .putString("access_token",access_token)
	         .putString("nickname",nickname)
	        .putString("headimgurl",headimgurl)
	         .putString("unionid",unionid)
	         .putString("token",presenter.getToken())
	         .putString("name",presenter.getToken())
	         .commit();  
		}else if(token==NORMAL_Token)
			normalPreferences.edit().putString("NORMAL_Token", NORMAL_Token);
		
	}
	
	@Override
	public void saveAllInfo() {
		sharedPreferences = getActivity().getSharedPreferences("ALLUSER_INFO",Context.MODE_PRIVATE);  
		   sharedPreferences.edit().putString("nickName", presenter.getUerNickName())
		   .putString("name", presenter.getUerNickName())
		   .putString("icon", presenter.getUserIcon())
		   .commit();
		   
		   
	}  

	
	

	@Override
	public void showToast() {
		// TODO Auto-generated method stub
		Toast.makeText(getActivity(), "请重新登陆", Toast.LENGTH_LONG).show();
	}

	@Override
	public String setNorToken() {

		return NORMAL_Token;
	}

	@Override
	public String setWXToken() {

		return WXToken;
	}

	/**
	 * 选择提示对话框
	 */
	private void ShowPickDialog() {
		new AlertDialog.Builder(getActivity())
				.setTitle("选择图片")
				.setNegativeButton("相册", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					
						Intent intent = new Intent(Intent.ACTION_PICK, null);
						
						/**
						 * 下面这句话，与其它方式写是一样的效果，如果：
						 * intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
						 * intent.setType(""image/*");设置数据类型
						 * 如果朋友们要限制上传到服务器的图片类型时可以直接写如："image/jpeg 、 image/png等的类型"
						 * 这个地方小马有个疑问，希望高手解答下：就是这个数据URI与类型为什么要分两种形式来写呀？有什么区别？
						 */
						intent.setDataAndType(
								MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
								"image/*");
						startActivityForResult(intent, 1);

					}
				})
				.setPositiveButton("拍照", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();
						/**
						 * 下面这句还是老样子，调用快速拍照功能，至于为什么叫快速拍照，大家可以参考如下官方
						 
						 */
						Intent intent = new Intent(
								MediaStore.ACTION_IMAGE_CAPTURE);
						//下面这句指定调用相机拍照后的照片存储的路径
						intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
								.fromFile(new File(Environment
										.getExternalStorageDirectory(),
										"xiaoma.jpg")));
						startActivityForResult(intent, 2);
					}
				}).show();
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		// 如果是直接从相册获取
		case 1:
			startPhotoZoom(data.getData());
			break;
		// 如果是调用相机拍照时
		case 2:
			File temp = new File(Environment.getExternalStorageDirectory()
					+ "/xiaoma.jpg");
			startPhotoZoom(Uri.fromFile(temp));
			break;
		// 取得裁剪后的图片
		case 3:
			/**
			 * 非空判断大家一定要验证，如果不验证的话，
			 * 在剪裁之后如果发现不满意，要重新裁剪，丢弃
			 * 当前功能时，会报NullException，小马只
			 * 在这个地方加下，大家可以根据不同情况在合适的
			 * 地方做判断处理类似情况
			 * 
			 */
			if(data != null){
				dataIntent = data;
				setPicToView(data);
				try {
					uploadPic(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +"HearderImage.png");
				} catch (Exception e) {
				Log.e("cuowu","出错了--------");
				}
				
			}
			break;
		default:
			break;

		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	
	
	/**
	 * 裁剪图片方法实现
	 * @param uri
	 */
	public void startPhotoZoom(Uri uri) {
		/*
		 * 至于下面这个Intent的ACTION是怎么知道的，大家可以看下自己路径下的如下网页
		 * yourself_sdk_path/docs/reference/android/content/Intent.html
		 * 直接在里面Ctrl+F搜：CROP ，之前小马没仔细看过，其实安卓系统早已经有自带图片裁剪功能,
		 * 是直接调本地库的，小马不懂C C++  这个不做详细了解去了，有轮子就用轮子，不再研究轮子是怎么
		 * 制做的了...吼吼
		 */
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		//下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 150);
		intent.putExtra("outputY", 150);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, 3);		
		Log.i("tag", "yao裁剪图片...");
	}
	/**
	 * 保存裁剪之后的图片数据
	 * @param picdata
	 */
	private void setPicToView(Intent picdata) {
		Bundle extras = picdata.getExtras();
		if (extras != null) {
			Bitmap photo = extras.getParcelable("data");
//			Drawable drawable = new BitmapDrawable(photo);
			Log.i("tag", "保存图片：正在打开文件...");
			try
			{
			FileOutputStream fileOutputStream = new FileOutputStream(new File( Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +"HearderImage.png"));
			Log.e("tag", Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +"HearderImage.png");
			ByteArrayOutputStream baops = new ByteArrayOutputStream();

			photo.compress(CompressFormat.PNG, 100, baops);

			Log.i("tag", "保存图片：正在保存数据...");

			fileOutputStream.write(baops.toByteArray());

			Log.i("tag", "保存图片：正在关闭输出流...");

			fileOutputStream.close();
		}
		catch (Exception e)
		{
			Log.e("tag", "保存图片：异常（" + e.getMessage() + "）");
			e.printStackTrace();			
		}

		Log.i("tag", "保存图片：保存成功");
		
		}
	}
	
	//上传
	private void uploadPic(String path) throws Exception{
		File file = new File(path);  
		Log.e("---1--","------1------");
		if (file.exists() && file.length() > 0) {  
			AsyncHttpClient client =  AsyncHttpCilentUtil.getInstance(getActivity());
		    RequestParams params = new RequestParams();  
		    params.put("file", file);  
		    Log.e("---2--","------2------");
		    getCookieText();
		 client.post(url, params, new TextHttpResponseHandler(){
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				// TODO Auto-generated method stub
				Log.e("---3--","------3------"+responseString+statusCode);
				 Log.e("URL",url);
				getCookieText();
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseString) {
				// TODO Auto-generated method stub
				Log.e("---4--","------4------");
				 try {
					 Log.e("BG返回信息", "response" + responseString);
					
					 Gson gson = new Gson();
					
					BGBean cBean = gson.fromJson(responseString,
							BGBean.class);
					 bgBean= (BGBean) cBean.clone();
					
					Log.d("BG返回信息"," bgBean---111-----"+bgBean.getOther());	
					data = bgBean.getData();
					Log.d("BG返回信息"," bgBean---mcp-----"+ bgBean.getData());
					int code = Integer.parseInt(bgBean.getOther().getCode());
					 if(200==code){
						 Toast.makeText(getActivity(), "上传成功", Toast.LENGTH_LONG).show();
					 }
					 
					 Log.e("BG返回信息", "success" + "success");
					 } catch (Exception e) {
					 e.printStackTrace();
					 }
				
			}

			
			 
		 });
		} else {  
		    Toast.makeText(getActivity(), "文件不存在", Toast.LENGTH_LONG).show();  
		} 
	}

		/** 
	     * 获取标准 Cookie   
	     */  
	    private String getCookieText() {  
	        PersistentCookieStore myCookieStore = new PersistentCookieStore(getActivity());  
	        List<Cookie> cookies = myCookieStore.getCookies();  
	        Log.d(TAG, "Se cookies.size() = " + cookies.size());  
	       
	        for (Cookie cookie : cookies) {  
	            Log.d(TAG, cookie.getName() + " set= " + cookie.getValue());  
	        }  
	         StringBuffer sb = new StringBuffer();  
	        for (int i = 0; i < cookies.size(); i++) {  
	             Cookie cookie = cookies.get(i);  
	             String cookieName = cookie.getName();  
	             String cookieValue = cookie.getValue();  
	            if (!TextUtils.isEmpty(cookieName)  
	                    && !TextUtils.isEmpty(cookieValue)) {  
	                sb.append(cookieName + " set=");  
	                sb.append(cookieValue + ";");  
	            }  
	        }  
	        Log.e("cookie setActivity", sb.toString());  
	        return sb.toString();  
	    }




		



	
	
}
